﻿using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using VisualWip.Core.Utils;
using VisualWip.PresentationModel.Text;

namespace VisualWip
{
    /// <summary>
    /// Interaction logic for RichTextViewer.xaml
    /// </summary>
    public partial class RichTextViewer : UserControl
    {
        private RichTextColumnViewModel _textColumn;

        public RichTextViewer()
        {
            InitializeComponent();
        }

        private void UserControlDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var columnViewModel = e.NewValue as RichTextColumnViewModel;
            if (columnViewModel != null)
            {
                _textColumn = columnViewModel;
                UpdateText();
                _textColumn.PropertyChanged += TextColumnPropertyChanged;
            }
            else
            {
                _textColumn.PropertyChanged -= TextColumnPropertyChanged;
                _textColumn = null;
            }
        }

        private void TextColumnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == PropertyHelper.Name(() => _textColumn.Document))
            {
                UpdateText();
            }
        }


        private void UpdateText()
        {
            TextViewer.Document = _textColumn.Document;
        }
    }
}